﻿//Added by deepak Kashyap

var dtCh = "/";
var minYear = 1900;
var maxYear = 2050;

function onFocusClear(obj, text) {
    if (obj.value == text) {
        obj.value = '';
    }
}

function trim(string) {
    if (string != null) {
        return string.replace(/^\s*|\s*$/g, "");
    }
    else {
        return string
    }
}


//function to validate page Numbers 
function OnPressValidatePageNum(obj) {


    var strNum = "1234567890"
    var objLen = trim(obj.value).length;
    var strObj = trim(obj.value).split('');
    var finalStr = "";
    for (var temp = 0; temp < objLen; temp++) {
        if (strNum.indexOf(strObj[temp]) == -1) {
            finalStr = finalStr;
        }
        else {
            if (finalStr == "") {
                finalStr = strObj[temp];
            }
            else {
                finalStr = finalStr + strObj[temp];
            }
        }
    }
    obj.value = finalStr;
    if (eval(obj.value) == 0) {
        obj.value = "1";
    }



}

//Function for Email validations  
//str email value
//str1 Message to be displayed  
function echeck(str, str1) {

    var at = "@"
    var dot = "."
    var lat = str.indexOf(at)
    var lstr = str.length
    var ldot = str.indexOf(dot)

    if (str.indexOf(at) == -1) {
        alert(str1);
        return false;
    }

    if (str.indexOf(at) == -1 || str.indexOf(at) == 0 || str.indexOf(at) == lstr) {
        alert(str1);
        return false;
    }

    if (str.indexOf(dot) == -1 || str.indexOf(dot) == 0 || str.indexOf(dot) == lstr) {
        alert(str1);
        return false;
    }

    if (str.indexOf(at, (lat + 1)) != -1) {
        alert(str1);
        return false;
    }

    if (str.substring(lat - 1, lat) == dot || str.substring(lat + 1, lat + 2) == dot) {
        alert(str1);
        return false;
    }

    if (str.indexOf(dot, (lat + 2)) == -1) {
        alert(str1);
        return false;
    }


    if (str.indexOf(" ") != -1) {
        alert(str1);
        return false;
    }
    if (str.indexOf(dot) + 1 == str.length) {
        alert(str1);
        return false;
    }

    return true;
}

//Validate on KeyUp
function validateKeyUp(e, act) {
    if (document.layers)
        Key = "e.which";
    else
        Key = "window.event.keyCode";
    switch (act) {
        case "alpha": re = /[^a-zA-Z ]+/i; break;
        case "alphaWithoutSpace": re = /[^a-zA-Z0-9]+/i; break;
        case "RegNo": re = /[^a-zA-Z0-9- ]+/i; break;
        case "numeric": re = /[^0-9]+/i; break;
        case "date": re = /[^0-9/]+/i; break;
        case "phone": re = /[^0-9-/]+/i; break;
        case "alphanumeric": re = /[^A-Za-z0-9 ]+/i; break;
        case "AlphaNumericSpl": re = /[^A-Za-z0-9 ,.-]+/i; break;
        case "AlphaNumericSplWithUnderscore": re = /[^A-Za-z0-9-_  #&*()<>./]+/i; break;
        case "LicenseNo": re = /[^A-Za-z0-9 \/\\/-]+/i; break;
        case "Floating": re = /[^0-9.]+/i; break;
        case "BloodGrp": re = /[^A-Za-z-+]+/i; break;
        case "inputWhtSpace": re = /[\s]+/i; break;
        case "alphaWithsplChar": re = /[^a-zA-Z-_ ,.\'&]+/i; break;
        case "IFSCCode": re = /[^A-Za-z0-9-]+/i; break;
    }
    if (re.test(e.value)) {
        e.value = e.value.replace(re, "")
        e.focus()
    }
}

function ChangeUpper(obj) {
    obj.value = obj.value.toUpperCase();
}

function isFloating(fieldValue) {
    var num = '0123456789.';
    var dot = '.';

    var i, j, found;
    var count = 0;
    found = false;

    for (i = 0; i < fieldValue.length; i++) {
        for (j = 0; j < num.length; j++) {
            if (fieldValue.charAt(i) == num.charAt(j)) {
                found = true; break;
            }
        }
        if (fieldValue.charAt(i) == dot.charAt(0)) {
            count = parseInt(count) + 1;
        }
        if (count > 1) {
            found = false;
        }
        if (found == false) { return false; }
        if (found == true) { found = false; }
    }

    return true;
}

function validateKeys(e, act) {
    if (document.layers)
        Key = "e.which";
    else
        Key = "window.event.keyCode";
    switch (act) {
        case "alpha": re = /[^a-zA-Z]+/i; break;
        case "numeric": re = /[^0-9]+/i; break;
        case "alphanumeric": re = /[^A-Za-z0-9]+/i; break;

    }
    if (re.test(eval("String.fromCharCode(" + Key + ")")))
        if ((eval(Key)) != 32) {
            eval(Key + "=null")
        }
}

function daysInFebruary(year) {
    return (((year % 4 == 0) && ((!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28);
}

function DaysArray(n) {
    for (var i = 1; i <= n; i++) {
        this[i] = 31
        if (i == 4 || i == 6 || i == 9 || i == 11) { this[i] = 30 }
        if (i == 2) { this[i] = 29 }
    }
    return this
}

function isValidDate(dt, divId, objHDN) {
    var divobj = document.getElementById(divId);
    var msg = "";
    if (trim(dt.value) != "" && trim(dt.value) != "MM/DD/YYYY") {
        var dtStr = dt.value;

        var daysInMonth = DaysArray(12)
        var pos1 = dtStr.indexOf(dtCh)
        var pos2 = dtStr.indexOf(dtCh, pos1 + 1)
        var strMonth = dtStr.substring(0, pos1)
        var strDay = dtStr.substring(pos1 + 1, pos2)

        var strYear = dtStr.substring(pos2 + 1)
        strYr = strYear
        if (strDay.charAt(0) == "0" && strDay.length > 1) strDay = strDay.substring(1)
        if (strMonth.charAt(0) == "0" && strMonth.length > 1) strMonth = strMonth.substring(1)
        for (var i = 1; i <= 3; i++) {
            if (strYr.charAt(0) == "0" && strYr.length > 1) strYr = strYr.substring(1)
        }
        month = parseInt(strMonth)
        day = parseInt(strDay)
        year = parseInt(strYr)
        if (pos1 == -1 || pos2 == -1) {
            msg = "The date format should be : MM/DD/YYYY.";
            if (IsAddSpan(divobj, msg)) {
                if (objHDN.value == "-1") objHDN.value = "0";
                objHDN.value = parseInt(objHDN.value) + 1;
                return true;
            }
            //alert("The date format should be : DD/MM/YYYY")
            //return false
        }
        else if (strMonth.length < 1 || month < 1 || month > 12) {
            msg = "Please Enter a Valid Month.";
            if (IsAddSpan(divobj, msg)) {
                if (objHDN.value == "-1") objHDN.value = "0";
                objHDN.value = parseInt(objHDN.value) + 1;
                return true;
            }
            //alert("Please enter a valid month")
            //return false
        }
        else if (strDay.length < 1 || day < 1 || day > 31 || (month == 2 && day > daysInFebruary(year)) || day > daysInMonth[month]) {
            msg = "Please enter a valid day.";
            if (IsAddSpan(divobj, msg)) {
                if (objHDN.value == "-1") objHDN.value = "0";
                objHDN.value = parseInt(objHDN.value) + 1;
                return true;
            }
            //alert("Please enter a valid day")
            //return false
        }
        else if (strYear.length != 4 || year == 0 || year < minYear || year > maxYear) {
            msg = "Please Enter a Valid 4 Digit Year Between " + minYear + " and " + maxYear + ".";
            if (IsAddSpan(divobj, msg)) {
                if (objHDN.value == "-1") objHDN.value = "0";
                objHDN.value = parseInt(objHDN.value) + 1;
                return true;
            }
            ///alert("Please enter a valid 4 digit year between "+minYear+" and "+maxYear)
            ///return false
        }
        else if (dtStr.indexOf(dtCh, pos2 + 1) != -1 || isInteger(stripCharsInBag(dtStr, dtCh)) == false) {
            msg = "Please enter a valid date.";
            if (IsAddSpan(divobj, msg)) {
                if (objHDN.value == "-1") objHDN.value = "0";
                objHDN.value = parseInt(objHDN.value) + 1;
                return true;
            }
            //alert("Please enter a valid date")
            //return false
        }
        else {
            if (IsRemoveSpan(divobj)) {
                objHDN.value = parseInt(objHDN.value) - 1;
            }
            return false;
        }
    }
    else {
        msg = "Please enter a valid date.";
        if (IsAddSpan(divobj, msg)) {
            if (objHDN.value == "-1") objHDN.value = "0";
            objHDN.value = parseInt(objHDN.value) + 1;
            return true;
        }
    }
}



function isInteger(s) {
    var i;
    for (i = 0; i < s.length; i++) {
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    return true;
}

function stripCharsInBag(s, bag) {
    var i;
    var returnString = "";
    for (i = 0; i < s.length; i++) {
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function daysInFebruary(year) {
    return (((year % 4 == 0) && ((!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28);
}

function DaysArray(n) {
    for (var i = 1; i <= n; i++) {
        this[i] = 31
        if (i == 4 || i == 6 || i == 9 || i == 11) { this[i] = 30 }
        if (i == 2) { this[i] = 29 }
    }
    return this
}

function IsRemoveSpan(objDIV) {
    //var d = document.getElementById(divtxtAddress1);
    if (objDIV != null) {
        var elm = 'Span' + objDIV.id;
        var olddiv = document.getElementById(elm);
        if (olddiv != null) {
            objDIV.removeChild(olddiv);
            return true;
        }
        else {
            return false;
        }
    }
}


function IsRemoveSpanCustomised(DIVId) {
    var objDIV = document.getElementById(DIVId);
    if (objDIV != null) {
        var elm = 'Span' + objDIV.id;
        var olddiv = document.getElementById(elm);
        if (olddiv != null) {
            objDIV.removeChild(olddiv);
            return true;
        }
        else {
            return false;
        }
    }
}



function isDate(dt) {

    if (trim(dt.value) != "" && trim(dt.value) != "MM/DD/YYYY") {
        var dtStr = dt.value;
        var dtch = '/';

        var daysInMonth = DaysArray(12)
        var pos1 = dtStr.indexOf(dtCh)
        var pos2 = dtStr.indexOf(dtCh, pos1 + 1)
        var strMonth = dtStr.substring(0, pos1)//for mm/dd/yy
        var strDay = dtStr.substring(pos1 + 1, pos2)

        var strYear = dtStr.substring(pos2 + 1)
        strYr = strYear
        if (strDay.charAt(0) == "0" && strDay.length > 1) strDay = strDay.substring(1)
        if (strMonth.charAt(0) == "0" && strMonth.length > 1) strMonth = strMonth.substring(1)
        for (var i = 1; i <= 3; i++) {
            if (strYr.charAt(0) == "0" && strYr.length > 1) strYr = strYr.substring(1)
        }
        month = parseInt(strMonth)
        day = parseInt(strDay)
        year = parseInt(strYr)
        if (pos1 == -1 || pos2 == -1) {
            alert("The Date Format Should be : MM/DD/YYYY")
            dt.value = "";
            return false

        }
        if (strMonth.length < 1 || month < 1 || month > 12) {

            alert("Please Enter a Valid Month.")
            dt.value = "";
            return false
        }
        if (strDay.length < 1 || day < 1 || day > 31 || (month == 2 && day > daysInFebruary(year)) || day > daysInMonth[month]) {
            alert("Please Enter a Valid Day.")
            dt.value = "";
            return false
        }
        if (strYear.length != 4 || year == 0 || year < minYear || year > maxYear) {
            alert("Please Enter a Valid 4 Digit Year Between " + minYear + " And " + maxYear + ".")
            dt.value = "";
            return false
        }
        if (dtStr.indexOf(dtCh, pos2 + 1) != -1 || isInteger(stripCharsInBag(dtStr, dtCh)) == false) {
            alert("Please Enter a Valid Date.")
            dt.value = "";
            return false
        }
    }
    return true
}


function ValidateFromToDates(objEffDateFrm, objEffDateTo) {
    if ((trim(objEffDateFrm.value) != "") && (trim(objEffDateTo.value) != "")) {
        if ((trim(objEffDateFrm.value) != "MM/DD/YYYY") && (trim(objEffDateTo.value) != "MM/DD/YYYY")) {

            var strFromdate = objEffDateFrm.value.split('/');


            var strTodate = objEffDateTo.value.split('/');

            if (eval(strTodate[2]) < eval(strFromdate[2])) {

                //Commented By Deepak
                //objEffDateFrm.focus();
                return false;
            }
            else if (eval(strTodate[0]) < eval(strFromdate[0]) && eval(strTodate[2]) == eval(strFromdate[2])) {

                //objEffDateFrm.focus();
                return false;
            }
            else if (eval(strTodate[1]) < eval(strFromdate[1]) && eval(strTodate[0]) == eval(strFromdate[0]) && eval(strTodate[2]) == eval(strFromdate[2])) {

                //objEffDateFrm.focus();
                return false;
            }


        }
    }

}
function ShowModalDialog(str) {
    var x = $find(str);
    Page_ClientValidate();
    if (!Page_IsValid)
        x.show();
}


function IsDecimal(obj, text) {
    var val = trim(obj.value).split('.');
    if (val.length > 2) {
        alert("Please Enter Valid Decimal Value in " + text + ".");
        obj.value = "";
        obj.focus();
        return false;
    }
}

function DecimalValdation(obj) {
    var txt = obj.value;
    //document.getElementById("TextBox1");

    var re = /[^0-9.]+/i;

    if (re.test(obj.value)) {
        obj.value = obj.value.replace(re, "")
        obj.focus()
    }
    var splitValue = txt.split(".");
    if (splitValue[1] != null) {
        var strVal = splitValue[0] + ".";
        if (splitValue[1].length > 2) {
            obj.value = strVal + splitValue[1].substring(0, 2);
        }
        else if (splitValue[1].length < 2) {
            obj.value;
        }
        if (splitValue.length > 2) {
            var strVal1 = splitValue[0] + ".";
            for (var i = 1; i < splitValue.length; i++) {

                strVal1 = strVal1 + splitValue[i];
            }
            obj.value = strVal1;
        }
    }
}
//----- Function Neither to take Numeric nor Alpahabets-------//
function NotAllowAnyOne(e) {
    if ((event.keyCode > 49 || event.keyCode < 57) && (event.keyCode > 65 || event.keyCode < 90)) {
        //bt.click(); 
        // alert("Enter Numbers Only");
        return false;
    }
}
//----- Function does not take space -------//
function NotAllowAnySpace(e) {
    if ((event.keyCode < 49 || event.keyCode > 57) && (event.keyCode < 65 || event.keyCode > 90) && (event.keyCode < 97 || event.keyCode > 122) && (event.keyCode != 48)) {
        //bt.click(); 
        // alert("Enter Numbers Only");
        return false;
    }
}


//---Added By Manish Singh[sd 30-may-2012]
//----- Function to validate numbers For textbox Control-------//
function AllowNumericOnly(e) {
    if ((event.keyCode < 49 || event.keyCode > 57) && (event.keyCode != 46) && (event.keyCode != 48)) {
        //bt.click(); 
        // alert("Enter Numbers Only");
        return false;
    }
}

function AllowNumOnly(e) {
    if ((event.keyCode < 49 || event.keyCode > 57) && (event.keyCode != 48)) {
        //bt.click(); 
        // alert("Enter Numbers Only");
        return false;
    }
}
//----- Function to validate numbers WITH ONR DOT IN textbox Control-------//
function AllowNumericWithDotOnly(e) {
    if ((event.keyCode < 49 || event.keyCode > 57) && (event.keyCode != 48) && (event.keyCode != 46)) {
        //bt.click(); 
        // alert("Enter Numbers Only");
        return false;
    }
}
//----- Function to validate numbers & Plus Sign For textbox Control-------//
function AllowNumericPlusOnly(e) {
    if ((event.keyCode < 49 || event.keyCode > 57) && (event.keyCode != 46) && (event.keyCode != 48) && (event.keyCode != 43)) {
        //bt.click(); 
        // alert("Enter Numbers Only");
        return false;
    }

}

//----- Function to validate aplphabets with dot & Hyphen For textbox Control-------//     
function AllowAlphabetOnly(e) {
    if ((event.keyCode < 65 || event.keyCode > 90) && (event.keyCode < 97 || event.keyCode > 122) && (event.keyCode != 46) && (event.keyCode != 32) && (event.keyCode != 45)) {
        //bt.click(); 
        //alert("Enter Aplhabets Only");
        return false;
    }

}
//--- End Added By Manish Singh[sd 30-may-2012]

//----- Function to validate aplphabets for address  For textbox Control-------//     
function AllowAlphabetAddOnly(e) {
    if ((event.keyCode < 65 || event.keyCode > 90) && (event.keyCode < 97 || event.keyCode > 122) && (event.keyCode != 46) && (event.keyCode != 32) && (event.keyCode != 45) && (event.keyCode < 49 || event.keyCode > 57) && (event.keyCode != 46) && (event.keyCode != 48) && (event.keyCode != 40) && (event.keyCode != 41)) {
        //bt.click(); 
        //alert("Enter Aplhabets Only");
        return false;
    }

}
//--- End Added By Manish Singh[sd 30-may-2012]


//----- Function to validate aplphabets & Numbers textbox Control-------//     
function AllowBoth(e) {
    if ((event.keyCode < 65 || event.keyCode > 90) && (event.keyCode < 49 || event.keyCode > 57) && (event.keyCode < 97 || event.keyCode > 122) && (event.keyCode != 46) && (event.keyCode != 32) && (event.keyCode != 45) && (event.keyCode != 48)) {
        //bt.click(); 
        //alert("Enter Aplhabets Only");
        return false;
    }

}
//--- End Added By Manish Singh[sd 30-may-2012]

function numericOnly(elementRef) {
    var keyCodeEntered = (event.which) ? event.which : (window.event.keyCode) ? window.event.keyCode : -1;

    if ((keyCodeEntered >= 48) && (keyCodeEntered <= 57)) {
        return true;
    }
        // '.' decimal point...
    else if (keyCodeEntered == 46) {
        // Allow only 1 decimal point ('.')...
        if ((elementRef.value) && (elementRef.value.indexOf('.') >= 0))
            return false;
        else
            return true;
    }

    return false;
}
//----Function to validate only one dot in textbox with accepting Number & Alphabet-------------//

function numalphaOnly(elementRef) {
    var keyCodeEntered = (event.which) ? event.which : (window.event.keyCode) ? window.event.keyCode : -1;

    if ((keyCodeEntered >= 48) && (keyCodeEntered <= 57) || (keyCodeEntered >= 65) && (keyCodeEntered <= 90) || (keyCodeEntered >= 97) && (keyCodeEntered <= 122) || (keyCodeEntered == 64)) {
        return true;
    }
        // '.' decimal point...
    else if (keyCodeEntered == 46) {
        // Allow only 1 decimal point ('.')...
        if ((elementRef.value) && (elementRef.value.indexOf('.') >= 0))
            return false;
        else
            return true;
    }

    return false;
}
// Code to restrict more than 3 selection in listbox
function CountItems(lb) {
    var count = 0;
    var lbl = document.getElementById('lRegistrationNo');
    for (var i = 0; i < lb.options.length; i++) {
        selectedFlag = lb.options.selectedIndex == -1;
        if (lb.options[i].selected) {
            count++;
        }
    }
    if (count > 3) {
        alert("You are only allowed to Select 3 items!");
        lb.selectedIndex = selectedFlag;
    }
}
//----- Function to validate Mobile No. in textbox Control-------//  
function MobileNumber(e) {
    if (document.getElementById('txtSimNo').value != "") {

        var y = document.getElementById('txtSimNo').value;
        if (isNaN(y) || y.indexOf(" ") != -1) {
            alert("Invalid Mobile No.");
            document.getElementById('txtSimNo').focus();
            return false;
        }

        if (y.length > 10 || y.length < 10) {
            alert("Mobile No. should be 10 digit");
            document.getElementById('txtSimNo').focus();
            return false;
        }
        if (!(y.charAt(0) == "9" || y.charAt(0) == "8" || y.charAt(0) == "7" || y.charAt(0) == "6")) {
            alert("Mobile No. should start with 9 or 8 ");
            document.getElementById('txtSimNo').focus();
            return false
        }
    }
}
// code to take textbox only numeric value
function isNumberOnlyWithoutZero(evt) {
    evt = (evt) ? evt : window.event;
    var charCode = (evt.which) ? evt.which : evt.keyCode;
    if (charCode > 31 && (charCode < 48 || charCode > 57) && charCode != 46 ) {
        return false;
    }
    return true;
}
// code to prevent back button 
function preventBackspace(e) {
    var evt = e || window.event;
    if (evt) {
        var keyCode = evt.charCode || evt.keyCode;
        if (keyCode === 8) {
            if (evt.preventDefault) {
                evt.preventDefault();
            } else {
                evt.returnValue = false;
            }
        }
    }
}